package com.pinyougou.shop.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description: 此方法的作用就是为了后面得到用户名信息
 * @Date: Create in 2019/7/12 11:49
 */
public class UserServiceImpl implements UserDetailsService {
    private SellerService sellerService;
    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.根据商家名得到商家对象
        TbSeller tbSeller = sellerService.findOne(username);
        System.out.println("userService:"  + tbSeller);
        //2.如果此商家存在，并且经过审核，再判断其是否有指定的角色
        if(tbSeller != null){
            if(tbSeller.getStatus().equals("1")){  //审核通过
                //3.添加角色到角色列表中
                List<GrantedAuthority> authorities = new ArrayList<>();
                authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
                return new User(username,tbSeller.getPassword(),authorities);
            }
        }
        //3.返回
        return null;
    }
}
